package com.itcast.mallweb.controller;

import com.itcast.admin.dto.CommodityDTO;
import com.itcast.admin.dto.SeckillDTO;
import com.itcast.admin.entity.Admin;
import com.itcast.admin.entity.Seckill;
import com.itcast.mallweb.service.SeckillService;
import com.itcast.result.Result;
import com.itcast.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/seckill")
@Slf4j
@Api(tags = "web-秒杀相关接口")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private AliOssUtil aliOssUtil;

    //接口已通过-OK
    @PostMapping("/add")
    @ApiOperation("新增秒杀商品")
    public Result addSeckill(SeckillDTO seckillDTO, @RequestParam("image") MultipartFile image) {
        log.info("新增秒杀商品的信息为：{}", seckillDTO);
        log.info("秒杀图片上传,{}", image.getOriginalFilename());
        try {
            //原始文件名
            String originalFilename = image.getOriginalFilename();
            //截取原始文件名的后缀 37-57cj.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//截取最后一个点的后半部分
            //构建新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件的请求路径
            String upload = aliOssUtil.upload(image.getBytes(), objectName);
            seckillDTO.setAvatar(upload);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        seckillService.addSeckill(seckillDTO);
        return Result.success();
    }
    //接口已通过-OK
    @GetMapping("/queryAll")
    @ApiOperation("查询全部秒杀商品")
    public Result<List<Seckill>> seckillQueryAll() {
        log.info("查询全部秒杀商品");
        List<Seckill> seckillList = seckillService.seckillQueryAll();
        return Result.success(seckillList);
    }
    //接口已通过-OK
    @GetMapping("/query")
    @ApiOperation("查询单个秒杀商品")
    public Result<Seckill> seckillQuery(@RequestParam Integer id) {
        log.info("查询单个秒杀商品的id为：{}",id);
        Seckill seckill = seckillService.seckillQuery(id);
        return Result.success(seckill);
    }
    //接口已通过-OK
    @PostMapping("/detele")
    @ApiOperation("批量删除秒杀商品")
    public Result seckillDetele(@RequestBody List<Long> ids) {
        log.info("批量秒杀商品的id为：{}",ids);
        seckillService.seckillDetele(ids);
        return Result.success();
    }

    @PostMapping("/amend")
    @ApiOperation("修改秒杀商品")
    public Result seckillAmend(SeckillDTO seckillDTO ,MultipartFile image) {
        log.info("修改秒杀商品的信息为：{}",seckillDTO);
        if (image != null) {
            try {
                //原始文件名
                String originalFilename = image.getOriginalFilename();
                //截取原始文件名的后缀 37-57cj.png
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//截取最后一个点的后半部分
                //构建新文件名称
                String objectName = UUID.randomUUID().toString() + extension;
                //文件的请求路径
                String upload = aliOssUtil.upload(image.getBytes(), objectName);
                seckillDTO.setAvatar(upload);
            } catch (IOException e) {
                log.error("文件上传失败：{}", e);
            }
        }
        seckillService.seckillAmend(seckillDTO);
        return Result.success();
    }

}
